package com.imoney.payment.web.pagination;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import com.imoney.framework.base.pagination.IPage;


/**
 * 
 * @author XAVIER
 * @date 2009-11-17
 * @version 1.0
 * 自定义标签库
 */
@SuppressWarnings("rawtypes")
public class PaginationHandler extends TagSupport {
	private static final long serialVersionUID = -423979721853479593L;
	private String toolClass="page-bottom";

	public int doStartTag() throws JspTagException {
		try {
			StringBuffer sb = new StringBuffer();
			
			sb.append("	<form id=\"queryForm\" name=\"queryForm\" method=\"post\" action=\"\">");
			pageContext.getOut().write( sb.toString() );
		}
		catch ( IOException ex ) {
			throw new JspTagException( "\u6267\u884C\u5206\u9875Handler\u9519\u8BEF" );
		}
		catch ( Exception e ) {
			e.printStackTrace();
		}
		
		return EVAL_BODY_INCLUDE;
	}
	
	
	
	/*
	 * 分页标签
	 */
	public int doEndTag() throws JspTagException {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			IPage page = (IPage) request.getAttribute("paginationTool");
			int beginIndex = page.getSize() * (page.getIndex() - 1);
			
			StringBuffer sb = new StringBuffer() ;
			
			sb.append("<input type=\"hidden\" name=\"page\" id=\"page\" value=\""+page.getIndex()+"\">" );
			sb.append("<span class=\"page-link\">");
			if (page.getIndex() >= 2) {
				sb.append("<a class=\"page-home form-element\" href=\"#\" onclick=\"gotoPage(1);return false;\">\u9996\u9875</a>&nbsp;&nbsp;");
				sb.append("<a class=\"page-previous form-element\" href=\"#\" onclick=\"gotoPage(" + (page.getIndex() <= 1?0:page.getIndex() -1) + ");return false;\">\u4E0A\u4E00\u9875</a>&nbsp;&nbsp;");
			}
			sb.append(beginIndex + 1).append(" - ").append( beginIndex + (page.getIndex() < page.getTotal()?page.getSize():page.getTotalRecord() - page.getSize() * (page.getTotal() - 1)) ).append("\u6761\uFF0C\u5171").append( page.getTotalRecord() ).append("条&nbsp;&nbsp;");
			if (page.getTotalRecord() > page.getSize() && page.getIndex() < page.getTotal()) {
				sb.append("<a class=\"page-next form-element\" href=\"#\" onclick=\"gotoPage(" + (page.getTotal() > page.getIndex()?page.getIndex() + 1: 0) + ");return false;\">\u4E0B\u4E00\u9875</a>&nbsp;&nbsp;");
				sb.append("<a class=\"page-end form-element\" href=\"#\" onclick=\"gotoPage(" + page.getTotal() + ");return false;\">\u5C3E\u9875</a>");
			}
			sb.append("</span>");
			sb.append("</form>");
			
			sb.append( "<script language=\"javascript\">" );
			sb.append( "function gotoPage(pageNum)" );
			sb.append( "{" );
			sb.append( "document.getElementById(\"page\").value=pageNum;");
			sb.append( "document.getElementById(\"queryForm\").submit();");
			sb.append( "}" );
			sb.append( "document.getElementById(\"queryForm\").action=\""+page.getUrl()+"\";");
			sb.append( "</script>" );
			pageContext.getOut().write( sb.toString() );
		}
		catch ( IOException ex )
		{
			throw new JspTagException( "\u6267\u884C\u5206\u9875Handler\u9519\u8BEF" );
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public String getToolClass() {
		return toolClass;
	}

	public void setToolClass(String toolClass) {
		this.toolClass = toolClass;
	}
	
}
